package com.we823.project.common.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.we823.project.common.web.status.StockAction;
import com.we823.project.model.repo.MaterialStockInModel;
import com.we823.project.model.repo.MaterialStockModel;
import com.we823.project.model.repo.MaterialStockOutModel;
import com.we823.project.repository.service.repo.MaterialStockService;
import com.we823.utils.web.DateUtil;

/**
 * 库存工具
 * 
 * @author ZhangYinxian
 *
 */
public class StockUtil {
	public static void changeStock(Date addtime, String materialCode,
			float totalNum, StockAction action,
			MaterialStockService materialStockService) {
		Calendar calendar = DateUtil.parse(addtime);

		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DATE);

		MaterialStockModel materialStockModel = materialStockService
				.findByMaterialCodeAndYearAndMonthAndDay(materialCode, year,
						month, day);
		// 根据当天时间查找时，说明当天无记录，则需要增加；
		// 无法查找则根据月，月查不到则根据年查找
		if (materialStockModel == null) {
			MaterialStockModel lastMaterialStockModel = null;
			Sort sort = new org.springframework.data.domain.Sort(
					Direction.DESC, "id");
			List<MaterialStockModel> materialStockModels = materialStockService
					.findByMaterialCodeAndYear(materialCode, year, sort);

			if (materialStockModels == null) {
				materialStockModels = materialStockService.findByMaterialCode(
						materialCode, sort);
			}

			if (materialStockModels != null && materialStockModels.size() > 0) {
				lastMaterialStockModel = materialStockModels.get(0);
			}
			materialStockModel = new MaterialStockModel();
			if (lastMaterialStockModel != null) {
				materialStockModel.setLastStock(lastMaterialStockModel
						.getStock());
				materialStockModel.setStock(materialStockModel.getLastStock());
			} else {
				materialStockModel.setLastStock(0);
				materialStockModel.setStock(0);
			}

		}

		materialStockModel.setAddtime(new Date());
		materialStockModel.setMaterialCode(materialCode);
		materialStockModel.setYear(year);
		materialStockModel.setMonth(month);
		materialStockModel.setDay(day);
		float stock = materialStockModel.getStock();
		if (StockAction.Out.equals(action)) {
			materialStockModel.setStock(stock - totalNum);
		} else {
			materialStockModel.setStock(stock + totalNum);
		}

		if (materialStockModel.getId() == null) {
			materialStockService.save(materialStockModel);
		} else {
			materialStockModel.setEdittime(new Date());
			materialStockService.update(materialStockModel);
		}
	}

	public static void changeStock(MaterialStockInModel materialStockInModel,
			MaterialStockService materialStockService) {
		if (materialStockInModel != null) {
			Date addtime = new Date();
			String materialCode = materialStockInModel.getMaterialCode();
			float totalNum = materialStockInModel.getTotalNum();
			StockAction action = StockAction.IN;
			changeStock(addtime, materialCode, totalNum, action, materialStockService);
		}
	}
	
	public static void changeStock(MaterialStockOutModel materialStockOutModel,
			MaterialStockService materialStockService) {
		if (materialStockOutModel != null) {
			Date addtime = new Date();
			String materialCode = materialStockOutModel.getMaterialCode();
			float totalNum = materialStockOutModel.getFinalNum();
			StockAction action = StockAction.Out;
			changeStock(addtime, materialCode, totalNum, action, materialStockService);
		}
	}
}
